var wantSale = {};
wantSale.countdown = 60; //倒计时时间
//我要保买
wantSale.ensureSaleBut=function(phoneBtnId){
	var phone = '';
	if(phoneBtnId != ''){
		phone =$("#"+phoneBtnId).val();
		if(phone=="" || phone=='请输入手机号！'){
			$("#"+phoneBtnId).val("请输入手机号！");
			$("#"+phoneBtnId).css('color','#FF7E00');
			return;
		}
		var regax = /^1[3456789]\d{9}$/;
	    if(!regax.test(phone)){
	    	$("#"+phoneBtnId).val("请输入正确手机号！");
			$("#"+phoneBtnId).css('color','#FF7E00');
	    	return
	    }
	}
	$("#phone").val(phone);
	  //初始化街道
    var html='<option value="">请选择街道号</option>';
    var html2='<option value="">请选择单元号</option>';
	$("#ensureSaleFrom #building").html(html);
	$("#ensureSaleFrom #unit").html(html2);
	$("#firstStep").addClass("show");
	$("#ensureSaleFrom").find(".pop-mask").show();
}
//获取验证码
wantSale.getVerificationCode=function(obj) {
	var phone =$("#lastStep #phone").val();
	if(phone ==""){
		$("#lastStep #phone").val("请输入手机号！");
		$("#lastStep #phone").css('color','#FF7E00');
		return
	}
	var regax = /^1[3456789]\d{9}$/;
	if(!regax.test(phone)){
		$("#lastStep #phone").val("请输入正确手机号！");
		$("#lastStep #phone").css('color','#FF7E00');
		return
	}
	$("#getVerificationCode").attr("disabled",true);
	$.ajax({
		type : "post",  
		dataType : "json",  
		data :{
			phone:phone
	    },  
	    url :baseUrl + "/ensureSale/getAuthentication?",  
	    success : function(data) {
	    	if(data ==1 || data >100 ){
	    		//获取成功
	    		wantSale.setTime($("#getVerificationCode"));
				$("#phone-check-error").text("");
	    	}else if(data ==-2){
	    		$("#phone-check-error").text("验证码发送失败,操作不正确！");
	    	}else if(data ==-3){
	    		$("#phone-check-error").text("验证码发送失败,验证次数过多！");
	    	}else{
	    		$("#phone-check-error").text("验证码发送失败！");
	    	}
	    	$("#getVerificationCode").attr("disabled",false);
	    }
	})
}
wantSale.setTime=function(obj){
	if (wantSale.countdown == 0) {
		obj.attr("saveType", false);
        obj.removeAttr("disabled");    
        obj.text("获取验证码");
        wantSale.countdown = 60;
        return;
    } else {
        obj.attr("disabled", true);
        obj.text("重新发送(" + wantSale.countdown + ")");
        wantSale.countdown--;
    }
    setTimeout(function() {
    	wantSale.setTime(obj) }
    ,1000)
}

//下一步
wantSale.nextStep=function(){
	if(!$("#ensureSaleFrom").valid()){
		return ;
	}
	$("#firstStep").removeClass("show");
	$("#lastStep").addClass("show");
}

//小区改变
wantSale.estateChange=function(refPlotId){
	$.ajax({
		url:baseUrl + "/plot/getStreetList",
		dataType : "json",
		type : 'post',
		async : false,
		data : {
			'parentId' : refPlotId,
		},
		success : function(data) {
			//操作街道
			var html="";
			html+='<option value="">请选择街道号</option>';
			for (var j = 0; j < data.length; j++) {
				html+='<option value="'+data[j].id+'">'+data[j].plotName+'</option>';
			}
			$("#ensureSaleFrom #building").html(html);
			$(".chosen-select").trigger("chosen:updated");
			$('.chosen-select').chosen({
				no_results_text: "没有找到结果！",//搜索无结果时显示的提示		
			});
		}
	});
}

//确认卖房
wantSale.confirmSale=function(){
	if($("#lastStep #phone").val() ==""){
		$("#lastStep #phone").val("请输入手机号！");
		$("#lastStep #phone").css('color','#FF7E00');
		return;
	}
	if($("#authenticationCode").val() =="" || $("#authenticationCode").val() =='请获取验证码！'){
		$("#authenticationCode").val("请获取验证码！");
		$("#authenticationCode").css('color','#FF7E00');
		return;
	}
	var  param =$("#ensureSaleFrom").serialize();
	var provinceCode=$("#firstStep #province").attr("code");
	var cityCode=$("#firstStep #city").attr("code");
	param+="&provinceCode="+provinceCode;
	param+="&cityCode="+cityCode;
	param+="&source=1";
	$.ajax({
	 	type : "post",  
        dataType : "json",  
        data :param,  
        url :baseUrl + "/ensureSale/saveEnsureSale",  
        success : function(data) {
        	if(data ==-1){
        		//必填项 非必填
        		$("#phone-check-error").text("保存失败，必填项不能为空！");
        	}else if(data ==-2){
        		//验证码不正确
        		$("#authenticationCode").val("请输入正确验证码！");
    			$("#authenticationCode").css('color','#FF7E00');
        	}else if(data ==-3){
        		//非法操作
        	}else if(data ==1){
        		//保存成功
        		$("#lastStep").removeClass("show");
        		wantSale.promptBox();
        	}
        }
	})
}

//弹出提示框
wantSale.promptBox=function(){
	$("#ensureSaleFrom").find(".pop-mask-success").addClass("active");
	$("#ensureSaleFrom").find(".js-pop-sell-success").addClass("show");
	//提示框关闭
	$("#ensureSaleFrom").find(".pop-close-success").unbind("").click(function(){
		$(".pop-mask-success").removeClass("active");
		$(this).parent().removeClass("show");
		$(".pop-close").click();
	})
	/*setTimeout(function (){
		$(".pop-close").click();
		$("#ensureSaleFrom").find(".pop-mask-success").removeClass("active");
		$("#ensureSaleFrom").find(".js-pop-sell-success").removeClass("show");
	},3000)*/
}

$(function (){
	//面积
	$.validator.addMethod("areaCheckSale", function(value, element, params) {
        if ((value != '') || (value != undefined)) {
            var regax = /^\d{1,8}(\.\d{1,2})?$/;
            return this.optional(element) || (regax.test(value));
        }
        return true;
    }, "请输入正确的面积,最多8位2位小数");
    //期望售价
	$.validator.addMethod("salesTotalPriceCheckSale", function(value, element, params) {
        if ((value != '') || (value != undefined)) {
            var regax = /^\d{1,8}(\.\d{1,2})?$/;
            return this.optional(element) || (regax.test(value));
        }
        return true;
    }, "请输入正确的售价,最多8位2位小数");
    //地址
	$.validator.addMethod("houseNumCheckSale", function(value, element, params) {
        var street =$("#firstStep #street").val();
        var unit=$("#firstStep #unit").val();
    	if ((unit != '') &&  (street != '')) {
           return true;
        }
        return false;
    }, "地址必填项");
	
	//提交房源校验手机号
	$("#lastStep #phone").focus(function(){
		  if($("#lastStep #phone").val() =='请输入手机号！' 
				|| $("#lastStep #phone").val() =='请输入正确手机号！'){
				$("#lastStep #phone").val("");
				$("#lastStep #phone").css('color','');
		  }
	});
	
	//获取验证码
	$("#authenticationCode").focus(function(){
		  if($("#authenticationCode").val() =='请获取验证码！' 
				|| $("#authenticationCode").val() =='请输入正确验证码！'){
				$("#authenticationCode").val("");
				$("#authenticationCode").css('color','');
		  }
	});
	
	//选择验房时间
	$("#secondStep .inspectionTime").click(function(){
		var inTime = $(this);
		if(inTime.attr("data-id") == '5'){
			$("#secondStep").find("li").find("div[class $='active']").removeClass("active");
			inTime.addClass("active");
		}else{
			$("#secondStep").find("li").eq(2).find(".inspectionTime").removeClass("active");
			if(inTime.hasClass("active")){
				inTime.removeClass("active");
			}else{
				inTime.addClass("active");
			}
		}
	});
	
	//表单校验
	$("#ensureSaleFrom").validate({
		ignore: '',
		onkeyup: false,
       	rules: {
       		estate: {
       			required: true,
       			maxlength: 50
       		},
       		area: {
       			required: true,
       			areaCheckSale:true
       		},
       		salesTotalPrice: {
       			required: true,
       			salesTotalPriceCheckSale:true
       		},
       		houseNum:{
       			required: true,
       			houseNumCheckSale:true,
       			maxlength: 50
       		}
        },
        errorPlacement: function(error, element) {
        	var msgArea;
        	msgArea = element.parent().parent().find("span[class*='li_pop_tishi']");
        	$(msgArea).attr('data-original-title',error.html());
	    	$(msgArea).attr('title',error.html());
	    	msgArea.show();
	    	$(msgArea).tooltip();
        },
        success: function(label,element) {
        	var msgArea;
    		msgArea = $(element).parent().parent().find("span[class*='li_pop_tishi']");
	    	msgArea.hide();
	    	$(msgArea).tooltip('destroy');
	    }
    });
	//关闭
	$("#ensureSaleFrom").find(".pop-close").click(function(){
		$("#ensureSaleFrom").find(".pop-mask").hide();
		$(this).parent().removeClass("show");
		$("#ensureSaleFrom")[0].reset();
		$(".li_pop_tishi").hide();
		//清空街道下拉
		var html1='<option value="">请选择街道号</option>';
		var html='<option value="">请选择单元号</option>';
		$("#ensureSaleFrom #building").html(html1);
		$("#ensureSaleFrom #unit").html(html);
		$(".chosen-select").trigger("chosen:updated");
		$(".chosen-select-unit").trigger("chosen:updated");
		//清空手机
		$("#ensureSale_phone").val("");
		$("#toolbar_sellphone").val("");
		//清空错误
		$("#phone-check-error").text("");
		//清空验房时间
		$("#secondStep").find("li").find("div[class $='active']").removeClass("active");
		$("#secondStep").find("li").find("div[data-id ='6']").addClass("active");
		//倒计时重置
	});
	
	//小区
	$("#ensureSaleFrom").find("#estate").autocomplete({
		source : function(request, response) {
			$.ajax({
				url:baseUrl + "/plot/getPlotList",
				dataType : "json",
				type : 'post',
				data : {
					'matchStr' : request.term,
					cityCode:$("#nowCity").attr("citycode")
				},
				success : function(data) {
					var arr = new Array(data.length);
					for (var i = 0; i < data.length; i++) {
						var s = {
							value : data[i].plotName,
							key: data[i].id
						};
						arr[i] = s;
					}
					response(arr); 
				}
			});
		},
		select : function(event, ui) {
			$("#ensureSaleFrom #estate").val(ui.item.value);
			$("#ensureSaleFrom #estateId").val(ui.item.key);
			event.preventDefault();
		},
		change:function (event, ui){
			if(ui.item == null){
				$("#ensureSaleFrom #estateId").val("");
			}
		}
	});
	
	//街道变
	$("#ensureSaleFrom #building").change(function(){
		var _this=$(this);
		$.ajax({
			url:baseUrl + "/plot/getUnitList",
			dataType : "json",
			type : 'post',
			async : false,
			data : {
				'parentId' : _this.val(),
			},
			success : function(data) {
				var html="";
				html+='<option value="">请选择单元号</option>';
				for (var i = 0; i < data.length; i++) {
					html+='<option value="'+data[i].id+'">'+data[i].plotName+'</option>';
				}
				$("#ensureSaleFrom #unit").html(html);
				$(".chosen-select-unit").trigger("chosen:updated");
				$('.chosen-select-unit').chosen({
					no_results_text: "没有找到结果！",//搜索无结果时显示的提示
				});
			}
		});
	});
})